<?php
require_once(APP_PATH.'/member/global.php');
class member_util_login{
	
	/**
	 * 设置cookie
	 *
	 * @param unknown_type $userinfo
	 * @param unknown_type $always
	 */
	var $sep = '#';
	public static function makeCookie($userinfo,$always = false){
		$SL = SecureLogin::getInstance();
		$_mycookie = $SL->encode_user_cookie($userinfo['uid'],$userinfo['email'],$userinfo['username'],time());
		setcookie(COOKIE_KEY,$_mycookie,$always?CURRENT_TIME+864000:null,'/',DOMAIN);
		setcookie('_uid',$userinfo['uid'],$always?CURRENT_TIME+864000:null,'/',DOMAIN);
		setcookie('username',$userinfo['username'],$always?CURRENT_TIME+864000:null,'/',DOMAIN);
		setcookie('email',$userinfo['email'],$always?CURRENT_TIME+864000:null,'/',DOMAIN);
	}
	
	public static function is_login(){
		$SL = SecureLogin::getInstance();
		if(!array_key_exists(COOKIE_KEY, $_COOKIE) or empty($_COOKIE[COOKIE_KEY])) {
			return false;
		}
		return $SL->decode_user_cookie();
	}
	
	/**
	 * 退出登录
	 *
	 */
	public static function logout(){
		if($_COOKIE[COOKIE_KEY]){
			setcookie(COOKIE_KEY,null,CURRENT_TIME-864000,'/',DOMAIN);
			setcookie('uid',null,CURRENT_TIME-864000,'/',DOMAIN);
			setcookie('username',null,CURRENT_TIME-864000,'/',DOMAIN);
			setcookie('email',null,CURRENT_TIME-864000,'/',DOMAIN);
		}
	}
	
	/**
	 * 根据用户uid获取会员相关信息
	 *
	 * @param unknown_type $uid
     * @param string $filed 返回某些字段的值,返回多个字段，就用","分隔字段名
	 * @return unknown
	 */
	public static function getMemberInfo($uid,$filed=null){
		if(is_numeric($uid)){
            $rs =  friends_util_cache::getUserInfo($uid);
            if($filed){
                if(strpos($filed,',') !== false){
                    $return = array();
                    foreach(explode(',',$filed) as $v){
                        if(in_array($v,array_keys($rs)))
                            $return[$v] = $rs[$v];
                    }
                    unset($rs);
                    return $return;
                }
                return $rs[$filed];
            }
            return $rs;
		}
	}
	
	/**
	 * 根据昵称获取会员信息  
	 * @param unknown_type $username
	 * @param unknown_type $fild
	 */
	public function getMemberInfoByName($username,$fild = null){
		if(!$username) return false;
		return Model::factory('member_model_data')->getUserByName($username,$fild);
	}
}
?>